const fs = require('fs');
const path = require('path');

class fsHelper {
    // 删除文件夹(递归)
    static rmdirSync_R(dirPath) {
        if (fsHelper.isDirectory(dirPath)) {
            console.error('删除', dirPath);
            let files = fs.readdirSync(dirPath);
            files.forEach((v, i) => {
                if (fs.statSync(dirPath + '/' + v).isDirectory()) {
                    fsHelper.rmdirSync_R(dirPath + '/' + v);
                } else {
                    fs.unlinkSync(dirPath + '/' + v);
                }
            })
            fs.rmdirSync(dirPath);
        } else {
            console.error('不是文件夹');
        }
    }
    // 复制文件夹(递归)
    static copyDirSync_R(srcDirPath, dstDirPath, isCover = false) {
        if (!fsHelper.isDirectory(srcDirPath)) {
            console.error('不存在原文件夹');
            return false;
        }
        // 覆盖原文件夹
        if (isCover && fsHelper.isDirectory(dstDirPath)) {
            console.log('rmdirSync_R', dstDirPath)
            fsHelper.rmdirSync_R(dstDirPath);
        }
        // 创建目标路径
        // if (fsHelper.mkdirSync_R(dstDirPath)) {
        // 遍历原文件夹，如果是文件夹就复制文件夹，如果是文件夹就复制文件
        let srcDirFd = fs.readdirSync(srcDirPath);
        srcDirFd.forEach((v, i) => {
            let subPath = srcDirPath + '/' + v;
            let dstSubPath = dstDirPath + '/' + v;
            if (fs.statSync(subPath).isDirectory()) {
                fsHelper.mkdirSync_R(dstSubPath);
                // 是文件夹
                fsHelper.copyDirSync_R(subPath, dstSubPath)
            } else if (fs.statSync(subPath).isFile()) {
                // 是文件
                console.log('subPath, dstSubPath', subPath, '\n', dstSubPath)
                fs.copyFileSync(subPath, dstSubPath);
            } else {
                console.error('无法识别该文件夹: ', srcDirPath + '/' + 'v');
            }
        })
        // } else {
        //     console.error('创建原路径失败');
        // }
    }
    // 创建文件夹(递归)
    static mkdirSync_R(dirPath) {
        console.log(dirPath)
        if (!fs.existsSync(dirPath) || !fs.statSync(dirPath).isDirectory(dirPath)) {
            mkdirSync_R(path.dirname(dirPath))
            fs.mkdirSync(dirPath)
        }
    }

    static isFile(path) {
        if (fs.existsSync(path) && fs.statSync(path).isFile()) {
            return true;
        }
        return false
    }
    static isDirectory(path) {
        if (fs.existsSync(path) && fs.statSync(path).isDirectory()) {
            return true;
        }
        return false;
    }
}

class zipHepler {
    static do_gzip(srcPath, destPath, completeCallback) {
        const archiver = require('archiver');
        const output = fs.createWriteStream(destPath);

        const archive = archiver('zip', {
            zlib: { level: 9 } // Sets the compression level.
        });
        output.on('close', function () {
            console.log(archive.pointer() + ' total bytes');
            if (completeCallback) completeCallback(null, archive.pointer());
        });

        output.on('end', function () {
            if (completeCallback) completeCallback(null, archive.pointer());
        });

        archive.on('warning', function (err) {
            if (completeCallback) completeCallback(err);
            if (err.code === 'ENOENT') {
                // log warning
            } else {
                // throw error
            }
        });

        archive.on('error', function (err) {
            if (completeCallback) completeCallback(err);
        });

        archive.pipe(output);

        if (fs.existsSync(srcPath)) {
            if (fs.statSync(srcPath).isDirectory()) {
                archive.directory(srcPath, false);
            } else if (fs.statSync(srcPath).isFile()) {
                archive.append(fs.createReadStream(srcPath), { name: path.basename(srcPath) });
            }
        }
        archive.finalize();
    }
}
module.exports = {
    fsHelper,
    zipHepler,
};
// let url = path.resolve('./');
// console.log(url)

// fsHelper.rmdirSync_R(url + '/res2 - 副本')

// fsHelper.mkdirSync_R(url + '/res2 - 副本/imports/haha')
// fsHelper.mkdirSync_R(url + '/res2 - 副本/imports/haha2')
// fsHelper.mkdirSync_R(url + '/res2 - 副本/imports/haha2/444')

// fsHelper.copyDirSync_R(path.dirname('./') + '/res2', 'D:/res')

// fs.copyFileSync(url + '/content.html', 'D:\\content.html12')